<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    var responseData = [
        {
            "type": "一类分公司",
            "0020": "广州",
            "0755": "深圳",
            "0769": "东莞",
            "0757": "佛山"
        },
        {
            "type": "二类分公司",
            "0752": "惠州",
            "0756": "珠海",
            "0760": "中山",
            "0750": "江门",
            "0754": "汕头",
            "0759": "湛江",
            "0663": "揭阳",
            "0758": "肇庆"
        },
        {
            "type": "三类分公司",
            "0763": "清远",
            "0751": "韶关",
            "0768": "潮州",
            "0668": "茂名",
            "0762": "河源",
            "0660": "汕尾",
            "0662": "阳江",
            "0753": "梅州",
            "0766": "云浮"
        }]
var keys=[]
        const initResponseDatakey = (responseData, keys) => {
    // 获取response的所有key 并生成tree供treeSelect使用
    if (responseData) {
      if (Object.prototype.toString.call(responseData) === '[object Array]') {
        // 是数组取第一项
        responseData = responseData[0];
      }
      let currentIndex = 0;
      for (const i in responseData) {
        if (Object.prototype.toString.call(responseData[i]) === '[object Array]') {
          keys.push({ title: i, value: i, children: [] });
 
          for (const j in responseData[i][0]) {
            keys[currentIndex].children.push({
              title: `${i}.${j}`,
              value: `${i}.cbzIndex.${j}`,
              children: [],
            });
          }
        } else if (Object.prototype.toString.call(responseData[i]) === '[object Object]') {
          keys.push({ title: i, value: i, children: [], disabled: true });
          let index = 0;
          for (const j in responseData[i]) {
            keys[0].children.push({ title: `${i}.${j}`, value: `${i}.${j}`, children: [] });
            if (
              Object.prototype.toString.call(responseData[i][j]) === '[object Object]' ||
              Object.prototype.toString.call(responseData[i][j]) === '[object Array]'
            ) {
              initResponseDatakey(responseData[i][j], keys[currentIndex].children[index++]);
            }
          }
        } else if (keys.children) {
          keys.children.push({ title: `${keys.title}.${i}`, value: `${keys.title}.cbzIndex.${i}` });
        } else {
          keys.push({ title: i, value: i });
        }
        currentIndex++;
      }
    }
    // setApiKey(allKey);
  };
 console.log( initResponseDatakey(responseData,keys));
</script>

</html>